#include <cmath>

#include "geom.h"
#include "TexturedEntity.h"
#include "ParabolicFlight.h"

ParabolicFlight::ParabolicFlight(TexturedEntity* entity, const float x0, const float y0, 
                                 const float Vx, const float yMax, const float factor)
: Behaviour(entity), m_elapsedTime(0.0f), m_x0(x0), m_y0(y0), m_Vx(Vx), m_factor(factor)
{
    m_Vy = sqrt(2.0f * GRAVITY * (yMax - m_y0));
}

ParabolicFlight::~ParabolicFlight(void)
{
}

void ParabolicFlight::prepare(float dt)
{
    // Calculate new position
    Vector3 newPosition;
    m_elapsedTime += (m_factor * dt);
    newPosition.x = m_x0 + (m_Vx * m_elapsedTime);
    newPosition.y = m_y0 + (m_Vy * m_elapsedTime) - (0.5f * GRAVITY * m_elapsedTime * m_elapsedTime);

    // Check if new position is out of screen bounds
    if (m_entity->isOutOfScreen())
    {
        m_entity->destroy();
    }

    // Set the entity's position
    m_entity->setPosition(newPosition);
}
